# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_admin.entity.vo.seedlings_vo import SeedlingsPageModel, SeedlingsModel
from module_admin.service.seedlings_service import SeedlingsService

seedlingsController = APIRouter(prefix='/seedlings/seedlings', dependencies=[Depends(LoginService.get_current_user)])


@seedlingsController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('seedlings:seedlings:list'))])
async def get_seedlings_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: SeedlingsPageModel = Depends( SeedlingsPageModel.as_query),
        data_scope_sql: str = Depends(GetDataScope('Seedlings'))
):
    seedlings_result = await SeedlingsService.get_seedlings_list(query_db, page_query, data_scope_sql)

    return ResponseUtil.success(model_content=seedlings_result)

@seedlingsController.get('/getById/{seedlingsId}', dependencies=[Depends(CheckUserInterfaceAuth('seedlings:seedlings:list'))])
async def get_seedlings_by_id(
        request: Request,
        seedlingsId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('Seedlings'))
):
    seedlings = await SeedlingsService.get_seedlings_by_id(query_db, seedlingsId)
    return ResponseUtil.success(data=seedlings)


@seedlingsController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('seedlings:seedlings:add'))])
@Log(title='seedlings', business_type=BusinessType.INSERT)
async def add_seedlings (
    request: Request,
    add_model: SeedlingsModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await SeedlingsService.add_seedlings(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@seedlingsController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('seedlings:seedlings:update'))])
@Log(title='seedlings', business_type=BusinessType.UPDATE)
async def update_seedlings(
    request: Request,
    edit_model: SeedlingsModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await SeedlingsService.update_seedlings(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@seedlingsController.delete('/delete/{seedlingsIds}', dependencies=[Depends(CheckUserInterfaceAuth('seedlings:seedlings:del'))])
@Log(title='seedlings', business_type=BusinessType.DELETE)
async def del_seedlings(
    request: Request,
    seedlingsIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = seedlingsIds.split(',')
    del_result = await SeedlingsService.del_seedlings(query_db, ids)
    return ResponseUtil.success(data=del_result)

@seedlingsController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('seedlings:seedlings:export'))])
@Log(title='seedlings', business_type=BusinessType.EXPORT)
async def export_seedlings(
    request: Request,
    seedlings_form: SeedlingsPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('Seedlings')),
):
    # 获取全量数据
    export_result = await SeedlingsService.export_seedlings_list(
        query_db, seedlings_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))